import { defineStore } from "pinia";
import { routes } from "@/router/index.ts";
import router from "@/router/index.ts";
// import {getrouteslist} from '@/Hook/getroutes'

let routes = localStorage.getItem("routeslist");
let routesdata: any = null;
if (routes) {
  routesdata = JSON.parse(routes);
}

export const routestore = defineStore({
  id: "routeslist",
  state: () => {
    return {
      routeslist: routesdata?.routeslist || "",
      num: routesdata?.num || 0,
    };
  },
  actions: {
    updateroutes(routelist: any) {
      this.routeslist = routelist;
    },
    addroutes(routes: any) {
      // console.log(router.getRoutes().length);
      // console.log(router.getRoutes());
      // console.log(routes);
      
      if (router.getRoutes().length == 8) {
        // console.log("路由重新添加");
        
        this.routeslist = routes;
        // console.log("this.routeslist",this.routeslist);
        this.routeslist.forEach((value) => {
          router.options.routes[2].children?.push(value);
          router.addRoute("home", value);
        });
        router.addRoute( {
          path: '/:pathMatch(.*)',
          redirect: '/error'
      },);
      }
    },
  },
  // 开启数据缓存
  persist: {
    //数据默认存在 sessionStorage 里，并且会以 store 的 id 作为 key
    enabled: true,
    strategies: [
      {
        key: "routeslist",
        storage: localStorage,
      },
    ],
  },
});
